package com.zshan.clinic.web.controller.clinic;


import com.zshan.clinic.admin.entity.dto.ClinicRoleDTO;
import com.zshan.clinic.admin.entity.vo.ClinicMenuTreeVo;
import com.zshan.clinic.admin.entity.vo.ClinicRoleVo;
import com.zshan.clinic.admin.service.clinic.ClinicAuthService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 权限相关
 */
@RestController
@RequestMapping("/clinic/auth")
public class ClinicAuthController extends AbstractController {

    @Autowired
    private ClinicAuthService clinicAuthService;

    /**
     * 获取菜单列表
     * @return
     * @param
     */
    @GetMapping("/getMenuList")
    public RestResp<List<ClinicMenuTreeVo>> getMenuList(){
        UserInfo userInfo = getUserInfo();
        List<ClinicMenuTreeVo> menuList = clinicAuthService.getMenuListByUserRole(userInfo.getEmpId(),userInfo.getIsAdmin());
        return RestResp.SUCCESS(menuList);
    }


    /**
     * 查询角色列表
     * @param
     * @return
     */
    @GetMapping("/getRoleList")
    public RestResp<PageInfo<ClinicRoleVo>> getRoleList(@RequestParam(name = "roleName",required = false) String roleName,
                                                        @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize){
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicRoleVo> pageInfo = clinicAuthService.getClinicRoleList(userInfo.getClinicId(),roleName,pageNo,pageSize);
        return RestResp.SUCCESS(pageInfo);
    }

    /**
     * 查询角色列表
     * @param
     * @return
     */
    @GetMapping("/getMenuIdListByRole")
    public RestResp<List<Long>> getMenuIdListByRole(@RequestParam(name = "roleId",required = false) Long roleId){
        UserInfo userInfo = getUserInfo();
        List<Long> roleIdLis = clinicAuthService.getMenuIdListByRole(userInfo.getClinicId(),roleId);
        return RestResp.SUCCESS(roleIdLis);
    }

    /**
     * 添加角色
     * @return
     */
    @PostMapping(value = "/addRole")
    public RestResp<Void> addRole(@RequestBody ClinicRoleDTO clinicRoleDTO) {
        UserInfo userInfo = getUserInfo();
        clinicRoleDTO.setCreateUserId(userInfo.getEmpId());
        clinicRoleDTO.setCreateBy(userInfo.getEmpName());
        clinicRoleDTO.setClinicId(userInfo.getClinicId());
        clinicAuthService.addRole(clinicRoleDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 修改角色
     * @return
     */
    @PostMapping(value = "/updateRole")
    public RestResp<Void> updateRole(@RequestBody ClinicRoleDTO clinicRoleDTO) {
        UserInfo userInfo = getUserInfo();
        clinicRoleDTO.setUpdateUserId(userInfo.getEmpId());
        clinicRoleDTO.setUpdateBy(userInfo.getEmpName());
        clinicRoleDTO.setClinicId(userInfo.getClinicId());
        clinicAuthService.updateRole(clinicRoleDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 删除角色
     * @return
     */
    @PostMapping(value = "/deleteRole")
    public RestResp<Void> deleteRole(@RequestBody ClinicRoleDTO clinicRoleDTO) {
        UserInfo userInfo = getUserInfo();
        clinicRoleDTO.setUpdateUserId(userInfo.getEmpId());
        clinicRoleDTO.setUpdateBy(userInfo.getEmpName());
        clinicRoleDTO.setClinicId(userInfo.getClinicId());
        clinicAuthService.deleteRole(clinicRoleDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 启用角色
     * @return
     */
    @PostMapping(value = "/enableRole")
    public RestResp<Void> enableRole(@RequestBody ClinicRoleDTO clinicRoleDTO) {
        UserInfo userInfo = getUserInfo();
        clinicRoleDTO.setUpdateUserId(userInfo.getEmpId());
        clinicRoleDTO.setUpdateBy(userInfo.getEmpName());
        clinicRoleDTO.setClinicId(userInfo.getClinicId());
        clinicAuthService.enableRole(clinicRoleDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 停用角色
     * @return
     */
    @PostMapping(value = "/disableRole")
    public RestResp<Void> disableRole(@RequestBody ClinicRoleDTO clinicRoleDTO) {
        UserInfo userInfo = getUserInfo();
        clinicRoleDTO.setUpdateUserId(userInfo.getEmpId());
        clinicRoleDTO.setUpdateBy(userInfo.getEmpName());
        clinicRoleDTO.setClinicId(userInfo.getClinicId());
        clinicAuthService.disableRole(clinicRoleDTO);
        return RestResp.SUCCESS();
    }
}
